package org.example.struct.array2;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/10/10 10:19
 * @apiNote
 */

public class Test00560sSubarraySum {

    public static void main(String[] args) {
        int[] nums = {1, 1, 1};
        int k = 2;
        System.out.println(test(nums, k));
    }
    public static int test(int[] nums, int k) {
        int n = nums.length;
        int count = 0, pre = 0;
        Map< Integer, Integer > map = new HashMap<>();
        map.put(0, 1);
        for(int i = 0; i < n; i++){
            pre += nums[i];
            if(map.containsKey(pre - k)){
                count += map.get(pre - k);
            }
            map.put(pre, map.getOrDefault(pre, 0) + 1);
        }
        return count;
    }
}
